home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / game / shoot / Orbit_src.lha / Orbit / source / rings.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-04  |  6.4 KB  |  302 lines

  1. /*
  2.     Amiga port by Oliver Gantert
  3.  
  4.     27.04.2000 - fixed some compiler warnings
  5. */
  6. /*
  7.  
  8. ORBIT, a freeware space combat simulator
  9. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  
  25. */
  26.  
  27. #include "orbit.h"
  28.  
  29. void InitRings()
  30. /*
  31.  *  Initialize rings
  32.  */
  33. {
  34.   int r;
  35.  
  36.   /* Jupiter */
  37.   ring[0].primary = 8;
  38.   ring[0].r1 = 102000.0 / KM_TO_UNITS1;
  39.   ring[0].r2 = 129130.0 / KM_TO_UNITS1;
  40.   strcpy (ring[0].fn, "maps/jupring.ppm");
  41.  
  42.   /* Saturn */
  43.   ring[1].primary = 13;
  44.   ring[1].r1 = 74400.0 / KM_TO_UNITS1;
  45.   ring[1].r2 = 140154.0 / KM_TO_UNITS1;
  46.   strcpy (ring[1].fn, "maps/satring.ppm");
  47.  
  48.   /* Uranus */
  49.   ring[2].primary = 21;
  50.   ring[2].r1 = 38949.0 / KM_TO_UNITS1;
  51.   ring[2].r2 = 50271.0 / KM_TO_UNITS1;
  52.   strcpy (ring[2].fn, "maps/uraring.ppm");
  53.  
  54.   /* Neptune */
  55.   ring[3].primary = 27;
  56.   ring[3].r1 = 42000.0 / KM_TO_UNITS1;
  57.   ring[3].r2 = 63000.0 / KM_TO_UNITS1;
  58.   strcpy (ring[3].fn, "maps/nepring.ppm");
  59.  
  60.   /* Read the textures, make display lists */
  61.   for (r=0; r<NRINGS; r++)
  62.   {
  63.     ReadRingTexture (r);
  64.     MakeRingList (r);
  65.   }
  66. }
  67.  
  68. void ReadRingTexture (int r)
  69. /*
  70.  *  Read texture for planetary rings
  71.  */
  72. {
  73.   int x, y, c, i;
  74.   FILE *fd;
  75.  
  76.   /* Get id for this texture */
  77.   glGenTextures (1, &ring[r].texid);
  78.   glBindTexture (GL_TEXTURE_2D, ring[r].texid);
  79.   glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
  80.  
  81.   /* Open the file */
  82.   if (NULL == (fd = fopen (ring[r].fn, "rb")))
  83.   {
  84.     Log ("Can't open %s, giving up!", ring[r].fn);
  85.     FinishSound();
  86.     CloseLog();
  87.     exit (0);
  88.   }
  89.  
  90.   /* Read past PPM header */
  91.   while (10 != fgetc(fd));
  92.   while (10 != fgetc(fd));
  93.   while (10 != fgetc(fd));
  94.  
  95.   /* Read in the color data */
  96.   for (y=0; y<8; y++)
  97.   {
  98.     for (x=0; x<255; x++)
  99.     {
  100.       for (i=0; i<3; i++)
  101.       {
  102.         c = fgetc (fd);
  103.         ring[r].tex[x][y][i] = 0xff & c;
  104.       }
  105.  
  106.       /* Don't forget alpha! */
  107.       ring[r].tex[x][y][3] = ring[r].tex[x][y][2];
  108.     }
  109.     
  110.     for (i=0; i<4; i++)
  111.     {
  112.       ring[r].tex[255][y][i] = 0;
  113.     }
  114.   }
  115.  
  116.   fclose (fd);
  117.  
  118.   /* Set the texture */
  119.   glTexImage2D (GL_TEXTURE_2D, 0, 4, 8, 256, 0, GL_RGBA,
  120.   GL_UNSIGNED_BYTE, ring[r].tex);
  121.   glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  122.   glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  123.   glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  124.   glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  125.   glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  126. }
  127.  
  128. static float MaterialColor[] = {
  129.   1.0, 1.0, 1.0, 1.0}
  130. ;
  131.  
  132. void MakeRingList (int r)
  133. /*
  134.  *  Make the display list for planetary rings
  135.  */
  136. {
  137.   double x, y, r1, r2, th, a;
  138.   int i, parity;
  139.  
  140.   /* Which side of the texture are we on */
  141.   parity = 0;
  142.  
  143.   /* Inner, outer radii */
  144.   r1 = ring[r].r1;
  145.   r2 = ring[r].r2;
  146.  
  147.   /* Angle of each sector */
  148.   a = 6.28 / ((double) ring_sectors);
  149.  
  150.   /* Get a display list */
  151.   ring[r].list = glGenLists (1);
  152.   glNewList (ring[r].list, GL_COMPILE);
  153.  
  154.   /* Don't write to depth buffer */
  155.   glDepthMask (GL_FALSE);
  156.  
  157.   /* Set up texture */
  158.   glBindTexture (GL_TEXTURE_2D, ring[r].texid);
  159.  
  160.   /* Do alpha blending here */
  161.   glEnable (GL_BLEND);
  162.   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  163.  
  164.   /* Rings are a quad strip */
  165.   glBegin (GL_QUAD_STRIP);
  166.  
  167.   for (i=0, th=0.0; i<=ring_sectors; i++, th+=a)
  168.   {
  169.     if (i == ring_sectors) th = 0.0;
  170.  
  171.     x = r2 * sin (th);
  172.     y = r2 * cos (th);
  173.     glNormal3f (0.0, 0.0, 1.0);
  174.     if (parity)
  175.     glTexCoord2f (0.0, 1.0);
  176.     else
  177.     glTexCoord2f (1.0, 1.0);
  178.     glVertex3d (x, y, 0.0);
  179.  
  180.     x = r1 * sin (th);
  181.     y = r1 * cos (th);
  182.     glNormal3f (0.0, 0.0, 1.0);
  183.     if (parity)
  184.     glTexCoord2f (0.0, 0.0);
  185.     else
  186.     glTexCoord2f (1.0, 0.0);
  187.     glVertex3d (x, y, 0.0);
  188.     
  189.     parity = !parity;
  190.   }
  191.   glEnd (); /* Quad strip */
  192.  
  193.   /* Now do one upside down */
  194.  
  195.   /***********
  196.  glPushMatrix();
  197.  glRotated (180.0, 0.0, 1.0, 0.0);
  198.  glBegin (GL_QUAD_STRIP);
  199.  
  200.  parity = 0;
  201.  
  202.  for (i=0, th=0.0; i<=ring_sectors; i++, th+=a)
  203.  {
  204.   if (i == ring_sectors) th = 0.0;
  205.  
  206.   x = r2 * sin (th);
  207.   y = r2 * cos (th);
  208.   glNormal3f (0.0, 0.0, 1.0);
  209.   if (parity)
  210.    glTexCoord2f (0.0, 1.0);
  211.   else
  212.    glTexCoord2f (1.0, 1.0);
  213.   glVertex3d (x, y, 0.0);
  214.  
  215.   x = r1 * sin (th);
  216.   y = r1 * cos (th);
  217.   glNormal3f (0.0, 0.0, 1.0);
  218.   if (parity)
  219.    glTexCoord2f (0.0, 0.0);
  220.   else
  221.    glTexCoord2f (1.0, 0.0);
  222.   glVertex3d (x, y, 0.0);
  223.   
  224.   parity = !parity;
  225.  }
  226.  glEnd ();
  227.  glPopMatrix();
  228. ***************/
  229.  
  230.   glDisable (GL_BLEND);
  231.   glDepthMask (GL_TRUE);
  232.   glEndList();
  233. }
  234.  
  235. float TransColor[4] = {
  236.   1.0, 1.0, 1.0, 0.5}
  237. ;
  238.  
  239. void DrawRings()
  240. /*
  241.  *  Render those pretty rings
  242.  */
  243. {
  244.   int r, p;
  245.   double v[3];
  246.  
  247.   /* Don't bother if Sparky doesn't want them */
  248.   if (!rings) return;
  249.  
  250.   /* Turn off lighting so rings are constant brightness */
  251.   glDisable (GL_LIGHTING);
  252.  
  253.   /* Set color to white, will be modulated with texture */
  254.   if (textures)
  255.   {
  256.     glColor4fv (MaterialColor);
  257.     /*  glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor); */
  258.   }
  259.   else
  260.   {
  261.     /*  glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, TransColor); */
  262.     glColor4fv (TransColor);
  263.   }
  264.  
  265.   for (r=0; r<NRINGS; r++)
  266.   {
  267.     p = ring[r].primary;
  268.  
  269.     /* Forget it if primary is hidden */
  270.     if (planet[p].hidden) continue;
  271.  
  272.     /* Don't bother if too far away */
  273.     if (planet[p].range2 > MAX_RING_RANGE) continue;
  274.  
  275.     glPushMatrix();
  276.  
  277.     /* Translate to planet */
  278.     Vsub (v, planet[p].pos, player.pos);
  279.     glTranslated (v[0], v[1], v[2]);
  280.  
  281.     /* Rotate for oblicity */
  282.     glRotated (planet[p].oblicity, 1.0, 0.0, 0.0);
  283.  
  284.     /* Turn off backface removal so we only have to draw rings
  285.      on one side */
  286.     glDisable (GL_CULL_FACE);
  287.  
  288.     /* Draw, pahdner! */
  289.     if (textures)
  290.     glEnable (GL_TEXTURE_2D);
  291.     else
  292.     glDisable (GL_TEXTURE_2D);
  293.     glCallList (ring[r].list);
  294.  
  295.     glDisable (GL_TEXTURE_2D);
  296.     if (textures) glEnable (GL_LIGHTING);
  297.     glEnable (GL_CULL_FACE);
  298.  
  299.     glPopMatrix();
  300.   }
  301. }
  302.